// pages/cart/cart.js
var that;
Page({
    
  /**
   * 页面的初始数据
   */
  data: {
    arr:[],
    done:false,
    num:0,
    price:0,
    check:false
  },
  del(){
    var that = this;
    if(this.data.num>=1){
      wx.showModal({
        title: '删除商品',
        content: '确认要删除这'+this.data.num+'件商品吗',
        success(res) {
          if (res.confirm) {
            if (that.data.arr.length < 1) {
              that.setData({
                check: false
              })
            }
            var arr = wx.getStorageSync('obj');
            for (var k = 0; k < that.data.arr.length; k++) {
              if (that.data.arr[k].check) {
                that.data.arr.splice(k,1)
                arr.splice(k, 1)
                k--;
                that.data.check=false;
              }
            }
            var price = 0;
            var a = 0;
            for (var k = 0; k < that.data.arr.length; k++) {
              if (that.data.arr[k].check) {
                a += 1;
                price += that.data.arr[k].priceCash * that.data.arr[k].num
              }
            }
            that.setData({
              num: a,
              arr: that.data.arr,
              price: price,
              check:that.data.check
            })
            wx.setStorageSync('obj', arr)

          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
  },
  jia(e){
    var arr = wx.getStorageSync('obj');
    var i=e.currentTarget.dataset.index;
    arr[i].num++;
    wx.setStorageSync('obj', arr)
    this.data.arr[i].num++;
    var price = 0;
    for (var k = 0; k < this.data.arr.length; k++) {
      if (this.data.arr[k].check) {
        price += this.data.arr[k].priceCash * this.data.arr[k].num
      }
    }
    this.setData({
      arr:this.data.arr,
      price: price
    })
  },
  jian(e) {
    var arr = wx.getStorageSync('obj');
    var i = e.currentTarget.dataset.index;
    if (this.data.arr[i].num<=1){
      this.data.arr.splice(i, 1)
      arr.splice(i,1);
      wx.setStorageSync('obj', arr)
      var price = 0;
      var a=0;
      for (var k = 0; k < this.data.arr.length; k++) {
        if (this.data.arr[k].check) {
          a += 1;
          price += this.data.arr[k].priceCash * this.data.arr[k].num
        }
      }
    }else{
      arr[i].num--;
      wx.setStorageSync('obj', arr)
      this.data.arr[i].num--;
      var price = 0;
      var a = 0;
      for (var k = 0; k < this.data.arr.length; k++) {
        if (this.data.arr[k].check) {
          a += 1;
          price += this.data.arr[k].priceCash * this.data.arr[k].num
        }
      }
    }
    var done=false;
    if (a == this.data.arr.length&&a!=0&&this.data.arr.length!=0) {
      done = true;
    } else {
      done = false
    }
    this.setData({
      num: a,
      arr: this.data.arr,
      price: price,
      check: done
    })
  },
  checked(){
    this.data.check=!this.data.check;
    var a = 0;
    var price = 0;
    for (var k = 0; k < this.data.arr.length; k++){
      this.data.arr[k].check = this.data.check;
      if (this.data.arr[k].check) {
        a += 1;
        price += this.data.arr[k].priceCash * this.data.arr[k].num
      }
    }
    this.setData({
      arr:this.data.arr,
      check:this.data.check,
      num: a,
      price: price,
    })

  },
  up(){
    this.setData({
      done:!this.data.done
    })
  },
  check(e){
    var i = e.currentTarget.dataset.index;
    this.data.arr[i].check = !this.data.arr[i].check;
    this.setData({
      arr: this.data.arr
    })
    var a=0;
    var price=0;
    if (this.data.arr[i].check){
      for(var k=0;k<this.data.arr.length;k++){
        if(this.data.arr[k].check){
          a+=1;
          price+=this.data.arr[k].priceCash*this.data.arr[k].num
        }
      }
    }else{
      for (var k = 0; k < this.data.arr.length; k++) {
        if (this.data.arr[k].check) {
          a += 1;
          price += this.data.arr[k].priceCash * this.data.arr[k].num
        }
      }
    }
    var done=false;
    if(a==this.data.arr.length){
        done=true;
    }else{
        done=false
    }
    this.setData({
      num: a,
      price:price,
      check:done
    })
    console.log(this.data.num)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that=this
    var arr=wx.getStorageSync('obj')
    for(var i=0;i<arr.length;i++){
      arr[i].check=false
    }
    this.setData({
      arr:arr
    })
    console.log(this.data.arr)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})